package com.king.base;

import com.google.common.base.Splitter;

import java.util.Map;

/**
 * Created by Administrator on 2015/4/23.
 */
public class SplitterDemo {
    public static void main(String[] args) {
        SplitterDemo splitterDemo = new SplitterDemo();
        splitterDemo.splitterTest1();
        splitterDemo.splitterTest2();
    }

    public  void splitterTest1() {
        //trim()移除字符串两侧的空白字符或其他预定义字符，omitEmptyStrings()忽略“”
        Iterable<String> splitResult = Splitter.onPattern("[',]{1,}").trimResults()
                .omitEmptyStrings().split("hello,world,世界,水平");
        for(String item : splitResult){
            System.out.println(item);
       }
        }
    //二次分割的例子
    public void splitterTest2() {
    String toSplitString = "a=b;c=d,e=f";
    Map<String, String> map = Splitter.onPattern("[,;]{1,}").withKeyValueSeparator('=').split(toSplitString);
    for (Map.Entry<String, String> entry : map.entrySet()) {
        System.out.println(String.format("%s=%s", entry.getKey(), entry.getValue()));
        }
    }
}
